import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {getToken} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
/* Layout */
import Layout from '@/layout'
import axios from 'axios'

NProgress.configure({showSpinner: false}) // NProgress Configuration

// no redirect whitelist
const whiteList = [
  '/login',
  '/ispReg',
  '/isvReg',
  '/resetPassword',
  '/findPassword',
  '/code',
  'sign'
]

const baseURL = process.env.VUE_APP_BASE_API || `${location.protocol}//${location.host}`
router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()

  if (to.path === '/dashboard') {
    var appKey = sessionStorage.getItem("appKey");
    if (appKey) {
      var flag = false;
      await axios.get(baseURL + '/portal/isv/getIsvPortal?appKey=' + appKey, {headers: {token: hasToken}})
        .then(res => {
          return res.data.data
        }).then(data => {
          flag = data.fileConfig
        })

      var path = []
      for (var i = 0; i < router.options.routes.length; i++) {
        var item = router.options.routes[i]
        path.push(item.path)
      }

      if (path.indexOf('/resource') === -1) {
        if (flag) {
          router.options.routes.push({
            path: '/resource',
            component: Layout,
            redirect: 'dashboard',
            children: [
              {
                path: '',
                name: 'Resource',
                component: () => import('@/views/resource/index'),
                meta: {
                  title: '资源管理',
                  icon: 'example'
                },
              },
            ]
          })
          router.addRoutes([{
            path: '/resource',
            component: Layout,
            redirect: 'dashboard',
            children: [
              {
                path: '',
                name: 'Resource',
                component: () => import('@/views/resource/index'),
                meta: {
                  title: '资源管理',
                  icon: 'example'
                },
              },
            ]
          }, {
            path: '*',
            redirect: '/404',
            hidden: true
          }])
          next({path: to.path})
          NProgress.done()
        }
      }
    }
  }

  if (hasToken) {
    if (to.path === '/') {
      next({path: '/isvs'})
      NProgress.done()
    }
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({path: '/'})
      NProgress.done()
    } else {
      next()
      // const hasGetUserInfo = store.getters.name
      // if (hasGetUserInfo) {
      //   next()
      // } else {
      //   try {
      //     // get user info
      //     await store.dispatch('user/getInfo')
      //
      //     next()
      //   } catch (error) {
      //     // remove token and go to login page to re-login
      //     await store.dispatch('user/resetToken')
      //     Message.error(error || 'Has Error')
      //     next(`/login?redirect=${to.path}`)
      //     NProgress.done()
      //   }
      // }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar

  NProgress.done()
})
